﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace LowLevelGraphics.ColorSpaces
{
    /// <summary>
    /// Use this to create your own color space
    /// </summary>
    //[DebuggerDisplay("L: {m_L}, a: {m_u}, b: {m_v}")]
    public class CustomizableColorSpace : AbstractColorSpace
    {
        protected Delegate m_ToColorDelegate = null;
        protected Delegate m_ToCustomizableColorSpaceDelegate = null;
        protected Delegate m_NormalizeToRGBColorDelegate = null;
        protected Delegate m_NormalizeDelegate = null;

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_color"></param>
        public CustomizableColorSpace(Color _color)
        {
            m_Color = _color;
        }

        /// <summary>
        /// Used by clone to create a deep copy
        /// </summary>
        /// <param name="_customizableColorSpace"></param>
        internal CustomizableColorSpace(CustomizableColorSpace _customizableColorSpace)
        {
            this.m_Color = _customizableColorSpace.m_Color;
            this.m_ToColorDelegate = _customizableColorSpace.m_ToColorDelegate;
            this.m_ToCustomizableColorSpaceDelegate = _customizableColorSpace.m_ToCustomizableColorSpaceDelegate;
            this.m_NormalizeDelegate = _customizableColorSpace.m_NormalizeDelegate;
            this.m_NormalizeToRGBColorDelegate = _customizableColorSpace.m_NormalizeToRGBColorDelegate;
        }

        /// <summary>
        /// Enable casting into operator
        /// </summary>
        /// <param name="_color"></param>
        /// <returns></returns>
        public static explicit operator CustomizableColorSpace(Color _color)
        {
            return new CustomizableColorSpace(_color);
        }

 
        /// <summary>
        /// Transform back to RGB color space delegate
        /// </summary>
        public Delegate ToColorDelegate
        {
            get { return m_ToColorDelegate; }
            set { m_ToColorDelegate = value; }
        }

        /// <summary>
        /// This has to be set to transform RGB color space into THIS
        /// </summary>
        public Delegate ToCustomizableColorSpaceDelegate 
        {
            get { return m_ToCustomizableColorSpaceDelegate; }
            set { m_ToCustomizableColorSpaceDelegate = value; }
        }

        /// <summary>
        /// Normalize color delegate
        /// </summary>
        public Delegate NormalizeToRGBColorDelegate
        {
            get { return m_NormalizeToRGBColorDelegate; }
            set { m_NormalizeToRGBColorDelegate = value; }
        }

        /// <summary>
        /// Normalize delegate
        /// </summary>
        public Delegate NormalizeDelegate
        {
            get { return m_NormalizeDelegate;}
            set { m_NormalizeDelegate = value; }
        }

        /// <summary>
        /// Normalize
        /// </summary>
        /// <returns></returns>
        public override AbstractColorSpace Normalize()
        {
            return (AbstractColorSpace)m_NormalizeDelegate.DynamicInvoke(m_Color);
        }

        /// <summary>
        /// Normalize to rgb color
        /// </summary>
        /// <returns></returns>
        public override Color NormalizeToRGBColor()
        {
            return (Color)m_NormalizeToRGBColorDelegate.DynamicInvoke(m_Color);
        }

        /// <summary>
        /// Retransform to rgb color space
        /// </summary>
        /// <returns></returns>
        public override Color ToColor()
        {
            return (Color)m_ToColorDelegate.DynamicInvoke(m_Color);
        }

        /// <summary>
        /// Use this to transform into this concrete color space
        /// </summary>
        protected override void CalcValues()
        {
            m_ToCustomizableColorSpaceDelegate.DynamicInvoke(m_Color);
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return base.ToString();
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <returns></returns>
        public override object[] GetValues()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Sets the values.
        /// </summary>
        /// <param name="_aObject">The _a object.</param>
        public override void SetValues(object[] _aObject)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Use this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CustomizableColorSpace(this);
        }
    }
}
